<?php

namespace App\Http\Controllers\VehicleAccounting;

use App\Http\Controllers\Controller;
use App\Models\Accounting\AccountingRate;
use Illuminate\Http\Request;

class AccountingRateController extends Controller
{
    public function rateList(Request $request)
    {
        return response()->formatJson(AccountingRate::query()->orderByDesc('created_at')->get()->toArray());
    }

    /**
     * 修改车辆信息
     *
     * @param  Request  $request
     *
     * @return mixed
     */
    public function updateRate(Request $request)
    {
        $params = $request->all();

        if ($params['insert']) {
            foreach ($params['insert'] as $k => &$v) {
                if (isset($v['_XID'])) {
                    unset($v['_XID']);
                }
                $v['created_at'] = date('Y-m-d H:i:s');
            }
            AccountingRate::query()->insert($params['insert']);
        }

        if ($params['update']) {
            foreach ($params['update'] as $k => $v) {
                $payImportTemplate = AccountingRate::query()->find($v['id']);
                $payImportTemplate->update($v);
            }
        }

        if ($params['delete']) {
            $ids = array_column($params['delete'], 'id');
            AccountingRate::destroy($ids);
        }

        return response()->formatJson(true, '修改成功');
    }
}
